# #将一级图像名称和对应的label信息写入到txt中

import os


listText = open('./example_01.txt', 'a+')  # 创建并打开一个txt文件，a+表示打开一个文件并追加内容

def write_txt(one_file):                    #定义一个写入函数
    image_src = one_file
    label_src = one_file
    image_file_name = os.path.abspath(image_src)
    label_src_name = os.path.abspath(label_src)
    image_file_name = image_file_name.split("/")[-1]
    label_src_name = label_src_name.split("/")[-1]
    # print(image_file_name)
    image_file_list = os.listdir(image_src)
    # print(image_file_list)
    for image_item in image_file_list:
        if image_item.endswith(".jpg"):
            image_item = image_item.strip()
            # print(image_item)
            image_path = os.path.join(image_file_name,image_item)
            print(image_path)
            listText.write(image_path)
            listText.write(" ")
            txt_name = image_item.split(".")[0] + ".txt"
            print(txt_name)
            txt_path = os.path.join(label_src_name,txt_name)
            print(txt_path)
            with open(txt_path,"r") as f:
                label = f.read()
                listText.write(label+"\n")
                print(label)




if __name__=="__main__":
 src = "example_01"   #一级目录
 write_txt(src)   #调用写入函数